<template>
  <div class="my-container">
    <!-- 已登录的区域 -->
    <div v-if="user" class="header user-info">
      <!-- 用户信息区域 -->
      <div class="base-info">
        <div class="left">
          <van-image class="avatar" round fit="cover" :src="userInfo.photo" />
          <span class="name">{{
            userInfo.name ? userInfo.name : '头条小智'
          }}</span>
        </div>
        <div class="right">
          <van-button size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <!-- 数据统计区域 -->
      <div class="data-stats">
        <div class="data-item">
          <div class="count">
            {{ userInfo.art_count ? userInfo.art_count : 0 }}
          </div>
          <div class="text">头条</div>
        </div>
        <div class="data-item">
          <div class="count">
            {{ userInfo.follow_count ? userInfo.follow_count : 0 }}
          </div>
          <div class="text">关注</div>
        </div>
        <div class="data-item">
          <div class="count">
            {{ userInfo.fans_count ? userInfo.fans_count : 0 }}
          </div>
          <div class="text">粉丝</div>
        </div>
        <div class="data-item">
          <div class="count">
            {{ userInfo.like_count ? userInfo.like_count : 0 }}
          </div>
          <div class="text">获赞</div>
        </div>
      </div>
    </div>
    <!-- 未登录区域 -->
    <div v-else class="header not-login">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/mobile.png" alt="" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- 宫格导航区域 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 页面导航区域 -->
    <van-cell class="cell-item" title="信息通知" is-link />
    <van-cell class="cell-item" title="小智同学" is-link />
    <van-cell
      v-if="user"
      class="login-cell"
      title="退出登录"
      @click="onLogout"
      clickable
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  name: 'MyIndex',
  components: {},
  props: {},
  data() {
    return {
      // 用户信息
      userInfo: {},
    }
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
  created() {
    // 如果用户登录了，则请求加载用户信息数据
    if (this.user) {
      this.loadUserInfo()
    }
  },
  mounted() {},
  methods: {
    // 退出登录的方法
    onLogout() {
      // 1.退出提示信息
      this.$dialog
        .confirm({
          title: '确认退出吗？',
          confirmButtonColor: '#3296FA',
          cancelButtonColor: 'red',
        })
        .then(() => {
          // 2.确认退出：清除登录状态（容器中的 user + 本地存储中的 user）
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // console.log('取消退出')
        })
    },
    // 获取用户信息的方法
    async loadUserInfo() {
      try {
        const { data: res } = await getUserInfo()
        // console.log(res.data)
        this.userInfo = res.data
      } catch (err) {
        this.$toast('获取数据失败，请稍后重试')
      }
    },
  },
}
</script>

<style lang="less">
.my-container {
  .header {
    height: 200px;
    background: url('~@/assets/banner.png');
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        width: 82px;
        height: 82px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 20px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 130px;
      // background-color: pink;
      padding: 35px 15px 0 15px;
      box-sizing: border-box;
      .left {
        display: flex;
        justify-content: center;
        align-items: center;
        .avatar {
          width: 62px;
          height: 62px;
          margin-right: 15px;
          border: 2px solid #fff;
        }
        .name {
          font-size: 18px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        height: 70px;
        color: #fff;
        .count {
          font-size: 22px;
        }
        .text {
          font-size: 14px;
        }
      }
    }
  }
  .grid-nav {
    .grid-item {
      height: 80px;
      margin-bottom: 5px;
      i.toutiao {
        font-size: 25px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 14px;
      }
    }
  }
  .login-cell {
    text-align: center;
    margin-top: 5px;
    color: #eb5253;
  }
  .cell-item,
  .login-cell {
    font-size: 16px;
  }
}
</style>

